package net.ustc.javamemcached.pipeline;

import net.ustc.javamemcached.memorystore.MemoryStoreManagerImpl;
import net.ustc.javamemcached.processor.IProtocolProcessor;
import net.ustc.javamemcached.protocol.Protocols;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.group.DefaultChannelGroup;

import java.nio.charset.Charset;
import java.util.Map;

/**
 */
public final class MemcachedPipelineFactory implements ChannelPipelineFactory {

	private MemoryStoreManagerImpl memoryManager;
	private String version;
	private boolean verbose;
	private int idleTime;

	private int frameSize;
	private DefaultChannelGroup channelGroup;
	private final MemcachedRequestDecoder memcachedRequestDecoder;
	private final MemcachedProtocolMessageHandler protocolMessageHandler;
	private final MemcachedResponseEncoder memcachedResponseEncoder;

	public MemcachedPipelineFactory(MemoryStoreManagerImpl memoryManager,
			Protocols protocols,
			Map<ChannelBuffer, IProtocolProcessor> processors, String version,
			boolean verbose, int idleTime, int frameSize,
			DefaultChannelGroup channelGroup) {
		this.memoryManager = memoryManager;
		this.version = version;
		this.verbose = verbose;
		this.idleTime = idleTime;
		this.frameSize = frameSize;
		this.channelGroup = channelGroup;
		memcachedRequestDecoder = new MemcachedRequestDecoder();
		protocolMessageHandler = new MemcachedProtocolMessageHandler(
				memoryManager, processors, version, verbose, idleTime,
				this.channelGroup);
		memcachedResponseEncoder = new MemcachedResponseEncoder();
	}

	public final ChannelPipeline getPipeline() throws Exception {
		return Channels.pipeline(new MemcachedRequestDecoder(),
				protocolMessageHandler, memcachedResponseEncoder);
	}

}
